
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
-- =============================================
-- Author:		LAXMAN S
-- Create date: 06-05-2012
-- Description:	Creating Customer
-- =============================================
CREATE PROCEDURE lgw.usp_Customer_PhoneNumbers_Create 
(@in_CustomerId bigint
,@in_BusinessPhone nvarchar(20)
,@in_ResidencePhone nvarchar(20)
,@in_Mobile1 nvarchar(12)
,@in_Mobile2 nvarchar(12)
,@in_isPrimary bit
,@in_CreatedUser nvarchar(25) 
,@in_UpdatedUser nvarchar(50)
)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

BEGIN TRY

    DECLARE @lv_InsertedId BIGINT
	CREATE TABLE #Temp(Id BIGINT)
	
	BEGIN TRAN
	
	UPDATE [lgw].[Customer_PhoneNumbers] SET isPrimary=0 WHERE [CustomerId]=@in_CustomerId
	
	INSERT INTO [lgw].[Customer_PhoneNumbers]
           ([CustomerId]
           ,[BusinessPhone]
           ,[ResidencePhone]
           ,[Mobile1]
           ,[Mobile2]
           ,[isPrimary]
			,CreatedUser  
			,UpdatedUser 
			)OUTPUT INSERTED.PhoneNumberId INTO #Temp
     SELECT
			@in_CustomerId
			,@in_BusinessPhone 
			,@in_ResidencePhone 
			,@in_Mobile1 
			,@in_Mobile2 
			,@in_isPrimary
			,@in_CreatedUser  
			,@in_UpdatedUser 

IF @@ERROR > 0
      BEGIN
        RAISERROR(N'Error While Creating Address', 16, 0)
      END
      
SELECT @lv_InsertedId=Id FROM #Temp


END TRY			
BEGIN CATCH 
	RETURN -99
END CATCH

IF @lv_InsertedId >0
BEGIN 
	COMMIT
END
ELSE BEGIN
	ROLLBACK
END

RETURN @lv_InsertedId
END


GO
